package com.aipolice.web;

import com.aipolice.pojo.Admin;
import com.aipolice.service.AdminService;
import com.aipolice.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.HtmlUtils;

@RestController
public class ForeRESTController {
    @Autowired
    AdminService adminService;
    @PostMapping("/foreregister")
    public Object register(@RequestBody Admin admin) {
        String username =  admin.getUsername();
        String password = admin.getPassword();
        username = HtmlUtils.htmlEscape(username);
        admin.setUsername(username);
        boolean exist =adminService.isExist(username);

        if(exist){
            String message ="用户名已经被使用,不能使用";
            return Result.fail(message);
        }

        admin.setPassword(password);

        adminService.add(admin);

        return Result.success();
    }
}
